package org.ztest.redis;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection;
import com.lambdaworks.redis.RedisConnectionPool;
import com.lambdaworks.redis.RedisURI;

public class TestLettuce
{
	private final int TEST_NUM = 100000;
	@SuppressWarnings("unused")
    private ExecutorService exe = Executors.newFixedThreadPool(50);
	
	public void test() throws InterruptedException, ExecutionException
	{
		RedisClient redisClient = new RedisClient(RedisURI.create("redis://192.168.19.134:6379/0"));

		RedisConnectionPool<RedisConnection<String, String>> pool = redisClient.pool(50, 50);
		
		Map<String, String> map = new HashMap<>();
		long start = System.currentTimeMillis();
		int i = 0;
		while (i++ < TEST_NUM)
		{
			final int val = i;
//			exe.submit(() -> {
//				try (RedisConnection<String, String> conn = pool.allocateConnection())
//				{
//					conn.set(String.valueOf(val), String.valueOf(val));
//				}
//				if (val == TEST_NUM)
//				{
//					System.out.println((System.currentTimeMillis() - start));
//				}
//			});
			map.put(String.valueOf(val), String.valueOf(val));
		}
		try (RedisConnection<String, String> conn = pool.allocateConnection())
		{
			conn.mset(map);
		}
		System.out.println((System.currentTimeMillis() - start));
	}
}
